package com.coscon.service.patternCommon.impl.mouse;

import cn.hutool.json.JSONUtil;
import com.coscon.enums.MouseActionTypeEnum;
import com.coscon.enums.MouseDirectionEnum;
import com.coscon.enums.MousePointerTypeEnum;
import com.coscon.model.IrcaCmd;
import com.coscon.model.PointCmd;
import com.coscon.service.patternCommon.ICommand;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.websocket.Session;
import java.awt.*;
import java.awt.event.InputEvent;

/**
 * 移动命令
 */
@Slf4j
public class MouseMoveCmdImpl implements ICommand {

    private Robot rb;
    //当前显示器分辨率
    private Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();

    private boolean debug = false;

    public MouseMoveCmdImpl(Robot rb){
        this.rb = rb;
    }



    public Boolean execute(IrcaCmd cmd,  Session session) {
        PointCmd pointCmd = JSONUtil.toBean(cmd.getValue(),PointCmd.class);
        //如果是手机触控，则执行
        if(pointCmd!=null &&  StringUtils.equalsIgnoreCase(pointCmd.getPointerType(), MousePointerTypeEnum.TOUCH.getKey() )){//
            log.debug(pointCmd.toString());
            //获取当前鼠标指针坐标
            Point location = MouseInfo.getPointerInfo().getLocation();
            //判断事件类型
            if(StringUtils.equalsIgnoreCase(MouseActionTypeEnum.TAP.getKey(),pointCmd.getType())){//单击
                log.debug("单击动作");
                rb.mousePress(InputEvent.BUTTON1_MASK);
                rb.mouseRelease(InputEvent.BUTTON1_MASK);
                rb.delay(50);
            }else if(StringUtils.equalsIgnoreCase(MouseActionTypeEnum.DOUBLE_TAP.getKey(),pointCmd.getType())){//双击
                log.debug("双击动作");
                rb.mousePress(InputEvent.BUTTON1_MASK);
                rb.mouseRelease(InputEvent.BUTTON1_MASK);
                rb.delay(100);
                rb.mousePress(InputEvent.BUTTON1_MASK);
                rb.mouseRelease(InputEvent.BUTTON1_MASK);
                rb.delay(50);
            } else if(StringUtils.equalsIgnoreCase(MouseActionTypeEnum.PRESS.getKey(),pointCmd.getType())){//长时间按住
                //鼠标右键
                rb.mousePress(InputEvent.BUTTON3_MASK);
                rb.mouseRelease(InputEvent.BUTTON3_MASK);
            }else{//up,down,left,right
                int x = location.x + pointCmd.getDeltaX()/6;
                int y = location.y + pointCmd.getDeltaY()/6;
                if(debug){
                    log.debug("新的坐标x=" + x + ",y=" + y);
                }
                if(x<0){
                    x = 0;
                }
                if(x>this.dimension.width){
                    x = this.dimension.width;
                }
                if(y<0){
                    y=0;
                }
                if(y>this.dimension.height){
                    y = this.dimension.height;
                }
                //移动到新的坐标
                rb.mouseMove(x, y);
                if(pointCmd.getDirection().equals(MouseDirectionEnum.DIRECTION_UP.getVal())){//up
                }else if(pointCmd.getDirection().equals(MouseDirectionEnum.DIRECTION_DOWN.getVal())){
                }else if(pointCmd.getDirection().equals(MouseDirectionEnum.DIRECTION_LEFT.getVal())){
                }else if(pointCmd.getDirection().equals(MouseDirectionEnum.DIRECTION_RIGHT.getVal())){
                }else if(pointCmd.getDirection().equals(MouseDirectionEnum.DIRECTION_HORIZONTAL.getVal())){
                }else if(pointCmd.getDirection().equals(MouseDirectionEnum.DIRECTION_VERTICAL.getVal())){
                }else if(pointCmd.getDirection().equals(MouseDirectionEnum.DIRECTION_NONE.getVal())){
                }
            }

        }//end if 手机触控
        return true;
    }


    @Override
    public Boolean executeByMac(IrcaCmd cmd, Session session) {
        return this.execute(cmd,session);
    }

    @Override
    public Boolean executeByWin(IrcaCmd cmd, Session session) {
        return this.execute(cmd,session);
    }

    @Override
    public Boolean executeByLinux(IrcaCmd cmd, Session session) {
        return this.execute(cmd,session);
    }

}
